package edu.udel.libra.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.udel.libra.model.League;
import edu.udel.libra.model.User;
import edu.udel.libra.model.repository.LeagueRepository;
import edu.udel.libra.model.repository.UserRepository;

@Controller
@RequestMapping("/admin")
public class AdminController extends AbstractController
{
	private static final Logger LOG = Logger.getLogger(AdminController.class.getName());
	
	private static final String LIST = "league_list";
	private static final String DENIED = "denied";
	
	@Autowired
	private LeagueRepository leagueRepository;
	
	@Autowired
	private UserRepository userRepository;
	
	/* League List view */
	@RequestMapping("")
	public String leagueList(Model model) {
		LOG.info("starting leagueList");
		
		User user = getUser();
		
		if (!user.isAdmin()) {
			return DENIED;
		}
		
		List<League> leagues = leagueRepository.getLeagues();
    
		model.addAttribute("user", user);
		model.addAttribute("leagues", leagues);
		return LIST;
	}
	
	/* User List view */
	@RequestMapping(value = "/users")
	public String userList(Model model) {
		LOG.info("starting userList");
		
		User user = getUser();
		
		if (!user.isAdmin()) {
			return DENIED;
		}
		
		List<League> leagues = leagueRepository.getLeagues();
		List<User> users = userRepository.getUsers();
		
		model.addAttribute("user", user);
		model.addAttribute("users", users);
		model.addAttribute("leagues", leagues);
		return LIST;
	}

}
